﻿//盘点 首页
var columns =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                          { display: '商品名称', name: 'ProductName', mintWidth: 80, width: 120 },
                          { display: '仓库名称', name: 'StorehouseName', mintWidth: 80, width: 120 },
                           { display: '库存数量', name: 'StockCount', mintWidth: 80, width: 120 }
             ];

var manager;
//init controls
$(document).ready(function () {
    initligerGrid();
});

function initligerGrid() {
    manager = null;
    manager = $("#StockListGrid").ligerGrid({
        columns: columns,
        title: '快速生成盘点',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: '99%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [ { text: '增加', click: selectStocksOperation, icon: 'add' }]},
        isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        url: "../Stock/GetPagerJsonOfStocks?rand=" + Math.random()
    });
}


//选择的库存
function selectStocksOperation() {


    var ids = checkedCustomer.join(',');
    var billId = $("#hidBillId").val();
    if (ids != null && ids != undefined && $.trim(ids).length > 0) {
        $.ligerDialog.confirm('确定快速生成盘点数据吗？', function (yes) {
            if (yes) {
                if (billId.length > 0) {
                    $.post("../ProductCheck/QuickCreate?rand="+Math.random(), { "billId": billId,"ids":ids }, function (data) {
                        if (data != undefined && data == "9") {
                            $.ligerDialog.success('生成成功！');
                            window.returnValue = ["ok"];
                            window.close();
                             return;
                        }
                    });

                }

            }
        });
    }
    else {
        if (!row) { $.ligerDialog.success('请选择需要删除行！'); return; }
    }
}





function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['CustomerID']);
        else
            removeCheckedCustomer(this.records[rowid]['CustomerID']);
    }
}

//========================================
/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/

var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.CustomerID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.ID);
    else removeCheckedCustomer(data.ID);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
